package com.longqinuo.mycarservice.utils;

import android.graphics.Color;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.longqinuo.mycarservice.R;

import java.util.List;
import java.util.Map;

/**
 * Created by zxw on 2016/5/24.
 */
public class MyCarsAdapter extends RecyclerView.Adapter<MyCarsAdapter.MyViewHolder>{
    private List<Map<String,Object>> mList;
    private int itemLayout;
    private OnItemClickListener onItemClickListener;
    public MyCarsAdapter(List<Map<String,Object>> mList,int itemLayout) {
        this.mList = mList;
        this.itemLayout = itemLayout;
    }
    public interface OnItemClickListener {
        void onItemClick(View itemView, int position);
    }
    public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(),itemLayout,null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        holder.code.setText((String)mList.get(position).get("code"));
        holder.carType.setText((String)mList.get(position).get("type"));
        boolean currentState = (Boolean) mList.get(position).get("state");
        holder.state.setText(currentState?"正常":"违章未处理");
        holder.state.setTextColor(currentState? Color.BLUE:Color.RED);
        holder.breakTimes.setText((String)mList.get(position).get("times"));
        holder.fineMoney.setText((String)mList.get(position).get("money"));
        holder.allGrade.setText((String)mList.get(position).get("grade"));
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
            this.code = (TextView) itemView.findViewById(R.id.tv_my_cars_code);
            this.carType = (TextView) itemView.findViewById(R.id.tv_my_cars_type);
            this.state = (TextView) itemView.findViewById(R.id.tv_my_cars_state);
            this.breakTimes = (TextView) itemView.findViewById(R.id.tv_my_cars_times);
            this.fineMoney = (TextView) itemView.findViewById(R.id.tv_my_cars_money);
            this.allGrade = (TextView) itemView.findViewById(R.id.tv_my_cars_grade);

        }

        public TextView code;
        public TextView carType;
        public TextView state;
        public TextView breakTimes;
        public TextView fineMoney;
        public TextView allGrade;
    }
}

